package cn.kgc.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {
    //定义名字
    //队列名字，两个队列，queue_inform_email queue_inform_sms
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";

    //交换机---》4种
    public static final String EXCHANGE_TOPICS_INFORM="exchange_topics_inform";


    /**
     * 交换机配置
     * ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
     * @return the exchange
     *
     * <bean id=“” class=""></bean>
     */
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange EXCHANGE_TOPICS_INFORM() {
       //durable(true)持久化，消息队列重启后交换机仍然存在
        //交换机类型： 广播fanout   一条消息给到交换机，把这个消息直接放到绑定此交换机的所有队列中
        //           工作direct   一条信息给到交换机，信息需要携带routing key（路由键），交换机跟队列所绑定的Bingdingkey匹配 ，匹配中则进入队列中
        //           发布订阅topic 一条信息给到交换机，信息需要携带routing key（路由键），交换机跟队列所绑定的Bingdingkey，bk是一个含有特殊匹配符的#，*匹配 ，匹配中则进入队列中

      //  return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
       // return ExchangeBuilder.fanoutExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }



    //声明队列
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS() {
        Queue queue = new Queue(QUEUE_INFORM_SMS);
        return queue;
    }

    //声明队列
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue QUEUE_INFORM_EMAIL() {
        Queue queue = new Queue(QUEUE_INFORM_EMAIL);
        return queue;
    }


    /** channel.queueBind(INFORM_QUEUE_SMS,"inform_exchange_topic","inform.#.sms.#");
     * 绑定队列到交换机 .
     * @param queue the queue
     * @param exchange the exchange
     * @return the binding
     */
    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_INFORM_SMS) Queue queue,
                                            @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        //                      短信队列      交换机           bindingkey 绑定key -->
        return BindingBuilder.bind(queue).to(exchange).with("inform.#.sms.#").noargs();
    }


    @Bean
    public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL) Queue queue,
                                              @Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("inform.#.email.#").noargs();
    }

}
